#include "cybertron/core/K8SDevOpsInfo.hpp"
#include "cybertron/core/SimOneEnv.hpp"
#include "cybertron/core/UtilUrlRequest.hpp"
#include "cybertron/core/JsonReader.hpp"
#include "cybertron/core/Log.hpp"
CYBERTRON_BEGIN

K8SDevOps::K8SDevOps() 
{
}

K8SDevOps::~K8SDevOps()
{
}

bool K8SDevOps::BindPod(string taskId)
{

	SimOneEnv::SimOneCloudEnvInfo envInfo;
	SimOneEnv::GetInstance().GetCloundEnvInfo(envInfo);
	SimOneEnv::SimOneK8SDevOpsInfo cloundServerInfo;
	SimOneEnv::GetInstance().GetSimOneK8SDevOpsInfo(cloundServerInfo);
	
	{
		string url = "http://" + cloundServerInfo.K8SDevOpsAddr + ":" + cloundServerInfo.K8SDevOpsPort + "/devops/internal/pod/bind";
		string postData = "{  \"task\":\"" + taskId + "\", \"pod\":\"" + envInfo.PodName + "\"}";
		logInfo("BindPod  Url:%s PostData:%s", url.c_str(), postData.c_str());
		try
		{
			std::string ret = UtilUrlRequest::PostUrl(url, postData);
			logInfo("GetUrl result:%s", ret.c_str());
		}
		catch (const UtilUrlRequest::TException& ex)
		{
			const char* error = ex.what();
			logInfo("GetUrl result:%s", error);
		}
	}

	return true;
}

bool K8SDevOps::DeleteSelfPod()
{
	SimOneEnv::SimOneCloudEnvInfo envInfo;
	SimOneEnv::GetInstance().GetCloundEnvInfo(envInfo);
	SimOneEnv::SimOneK8SDevOpsInfo cloundServerInfo;
	SimOneEnv::GetInstance().GetSimOneK8SDevOpsInfo(cloundServerInfo);

	string url = "http://" + cloundServerInfo.K8SDevOpsAddr + ":" + cloundServerInfo.K8SDevOpsPort + "/devops/internal/pod/delete";
	string postData = "{  \"name\":\"" + envInfo.PodName + "\", \"namespace\":\"" + envInfo.PodNameSpace + "\"}";
	logInfo("DeleteSelfPod Url:%s PostData:%s", url.c_str(), postData.c_str());
	try
	{
		//std::string ret = UtilUrlRequest::GetUrl(url);
		std::string ret = UtilUrlRequest::PostUrl(url, postData);
		logInfo("geturl result:%s", ret.c_str());
	}
	catch (const UtilUrlRequest::TException& ex)
	{
		const char* error = ex.what();
		logInfo("geturl result:%s", error);
	}
	return true;
}

bool K8SDevOps::QueryPod(string podName, PodStatus& Status)
{
		
	SimOneEnv::SimOneCloudEnvInfo envInfo;
	SimOneEnv::GetInstance().GetCloundEnvInfo(envInfo);
	SimOneEnv::SimOneK8SDevOpsInfo cloundServerInfo;
	SimOneEnv::GetInstance().GetSimOneK8SDevOpsInfo(cloundServerInfo);
	
	string podNameParam = "pod=" + envInfo.PodName + "&";
	string podNamespaceParam = "namespace=" + envInfo.PodNameSpace;
	string url = "http://" + cloundServerInfo.K8SDevOpsAddr + ":" + 
		cloundServerInfo.K8SDevOpsPort + "/devops/internal/pod/status?" + 
		podNameParam + podNamespaceParam;
	logInfo("QueryPod url:%s", url.c_str());
	std::string ret;
	try
	{
		ret = UtilUrlRequest::GetUrl(url);
		logInfo("GetUrl result:%s", ret.c_str());
	}
	catch (const UtilUrlRequest::TException& ex)
	{
		const char* error = ex.what();
		logInfo("GetUrl result Error:%s", error);
		return false;
	}

	nlohmann::json worldJsonRoot;
	if (!JsonReader::loadString(worldJsonRoot, ret))
	{
		return false;
	}
	int code = JsonReader::getInt(worldJsonRoot, "code");
	if (code != 0) {
		return false;
	}

	string content = JsonReader::getString(worldJsonRoot, "content");
	if (content == "Pending") {
		Status = PodStatus_Pending;
	}
	else if (content == "Running") {
		Status = PodStatus_Running;
	}
	else if (content == "Succeeded") {
		Status = PodStatus_Succeeded;
	}
	else if (content == "Failed") {
		Status = PodStatus_Failed;
	}
	else if (content == "Unknown") {
		Status = PodStatus_Unknown;
	}
	else {
		Status = PodStatus_Unknown;
		logError("QueryPod UnKnow :%s", content.c_str());

	}
	
	logInfo("CheckPodIsRunning url:%s left", url.c_str());
	return true;
}

CYBERTRON_END